package day19.feifei;

public class Product {
    private Integer number;
    private String name;

    public Product() {
    }

    public Product(Integer number, String name) {
        this.number = number;
        this.name = name;
    }

    /**
     * 获取
     *
     * @return number
     */
    public Integer getNumber() {
        return number;
    }

    /**
     * 设置
     *
     * @param number
     */
    public void setNumber(Integer number) {
        this.number = number;
    }

    /**
     * 获取
     *
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    public String toString() {
        return "Product{number = " + number + ", name = " + name + "}";
    }
}

class ProducerThread extends Thread {
    //共享
    private Product product;

    public ProducerThread() {
    }

    public ProducerThread(Product product, String name) {
        super(name);
        this.product = product;
    }

    @Override
    public void run() {

            synchronized (product){//在程序中product只创建一次
                //生产，判断有没有值
                if(product.getNumber()==0){
                    product.setNumber(1);
                    //通知消费者线程消费
                    product.notify();
            }else {
                    //不能生产
                    //进入等待状态
                    try {
                        product.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
        }
    }

    /**
     * 获取
     *
     * @return product
     */
    public Product getProduct() {
        return product;
    }

    /**
     * 设置
     *
     * @param product
     */
    public void setProduct(Product product) {
        this.product = product;
    }

    public String toString() {
        return "ProducerThread{product = " + product + "}";
    }
}